package com.qf.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qf.entity.Goods;
import com.qf.service.IGoodsService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Value("${es.esExchangeName}")
    private String exchangeName;

    @RequestMapping("/getGoodsPage")
    public Page<Goods> getGoodsPage(@RequestBody Page<Goods> page) {
        return goodsService.selectPage(page);
    }

    @RequestMapping("/addGoods")
    public Boolean addGoods(@RequestBody Goods goods) {
        boolean insert = goodsService.insert(goods);
        if (insert) {
            // 把商品的信息发送给MQ，同步到ES
            rabbitTemplate.convertAndSend(exchangeName, "es.insert", goods);
        }
        return insert;
    }

    @RequestMapping("/getGoodsMap")
    public Map<Integer, Goods> getGoodsMap(@RequestBody List<Integer> gidList) {
        return goodsService.getGoodsMap(gidList);
    }
}
